﻿using Linyou.Net7.WebMinimalAPI.Utility.SwaggerExt;
using Microsoft.AspNetCore.Mvc;

namespace Linyou.Net7.WebMinimalAPI
{
    public static class UserManager
    {
        public static void UserAPI(this WebApplication app)
        {
            typeof(UserApiRoute).GetEnumNames().ToList().ForEach(name =>
            {
                string route = name.ToLower();
                if (route.Contains("get"))
                {
                    app.MapGet(name, (HttpContext httpContext, int? Id) =>
                    {
                        if (Id is null || Id >= 100)
                        {
                            return new
                            {
                                Id = string.Empty,
                                Name = "Linyou",
                                Status = false,
                                Code = 400,
                                Message = "该用户ID未存在数据库，请联系数据管理员处理"
                            };
                        }
                        else
                        {
                            return new
                            {
                                Id = $"{new Random().NextInt64(0, 999999):000000}",
                                Name = "Linyou",
                                Status = true,
                                Code = 200,
                                Message = "OK"
                            };
                        }
                    })
                 .WithName(name)
                 .WithGroupName(nameof(ApiVersions.BaseServices))
                 .WithOpenApi();
                }
                if (route.Contains("post"))
                {
                    app.MapPost(name, (HttpContext httpContext, int? Id) =>
                    {
                        if (Id is null || Id >= 100)
                        {
                            return new
                            {
                                Id = string.Empty,
                                Name = "Linyou",
                                Status = false,
                                Code = 400,
                                Message = "该用户ID未存在数据库，请联系数据管理员处理"
                            };
                        }
                        else
                        {
                            return new
                            {
                                Id = $"{new Random().NextInt64(0, 999999):000000}",
                                Name = "Linyou",
                                Status = true,
                                Code = 200,
                                Message = "OK"
                            };
                        }
                    })
                     .WithName(name)
                     .WithGroupName(nameof(ApiVersions.BaseServices))
                     .WithOpenApi();
                }
                if (route.Contains("put"))
                {
                    app.MapPut(name, (HttpContext httpContext, int? Id) =>
                    {
                        if (Id is null || Id >= 100)
                        {
                            return new
                            {
                                Id = string.Empty,
                                Name = "Linyou",
                                Status = false,
                                Code = 400,
                                Message = "该用户ID未存在数据库，请联系数据管理员处理"
                            };
                        }
                        else
                        {
                            return new
                            {
                                Id = $"{new Random().NextInt64(0, 999999):000000}",
                                Name = "Linyou",
                                Status = true,
                                Code = 200,
                                Message = "OK"
                            };
                        }
                    })
                     .WithName(name)
                     .WithGroupName(nameof(ApiVersions.BaseServices))
                     .WithOpenApi();
                }
                if (route.Contains("delete"))
                {
                    app.MapDelete(name, (HttpContext httpContext, int? Id) =>
                    {
                        if (Id is null || Id >= 100)
                        {
                            return new
                            {
                                Id = string.Empty,
                                Name = "Linyou",
                                Status = false,
                                Code = 400,
                                Message = "该用户ID未存在数据库，请联系数据管理员处理"
                            };
                        }
                        else
                        {
                            return new
                            {
                                Id = $"{new Random().NextInt64(0, 999999):000000}",
                                Name = "Linyou",
                                Status = true,
                                Code = 200,
                                Message = "OK"
                            };
                        }
                    })
                     .WithName(name)
                     .WithGroupName(nameof(ApiVersions.BaseServices))
                     .WithOpenApi();
                }
            });
            if (false)
            {
                app.MapGet("GetUser", (HttpContext httpContext, int? Id) =>
                {
                    if (Id is null || Id >= 100)
                    {
                        return new
                        {
                            Id = string.Empty,
                            Name = "Linyou",
                            Status = false,
                            Code = 400,
                            Message = "该用户ID未存在数据库，请联系数据管理员处理"
                        };
                    }
                    else
                    {
                        return new
                        {
                            Id = $"{new Random().NextInt64(0, 999999):000000}",
                            Name = "Linyou",
                            Status = true,
                            Code = 200,
                            Message = "OK"
                        };
                    }
                })
           .WithName("GetUser")
           .WithGroupName(nameof(ApiVersions.BaseServices))
           .WithOpenApi();

                app.MapGet("/GetUserName", (HttpContext httpContext, string Name) =>
                {
                    return new
                    {
                        Id = $"{new Random().NextInt64(0, 999999):000000}",
                        Name = "Linyou",
                        Status = true,
                        Code = 200
                    };
                })
                 .WithName("GetUserName")
                 .WithGroupName(nameof(ApiVersions.BaseServices))
                 .WithOpenApi();

                app.MapGet("/GetOrg", (HttpContext httpContext, int OrgId, string Name) =>
                {
                    return new
                    {
                        Id = $"{new Random().NextInt64(0, 999999):000000}",
                        Name = "Linyou",
                        Status = true,
                        Code = 200
                    };
                })
                .WithName("GetOrg")
                .WithGroupName(nameof(ApiVersions.BaseServices))
                .WithOpenApi();

                app.MapPost("/PostUser", ([FromServices] HttpContext httpContext) =>
                {
                    return new
                    {
                        Id = $"{new Random().NextInt64(0, 999999):000000}",
                        Name = "Linyou",
                        Status = true,
                        Code = 200
                    };
                })
               .WithName("PostUser")
               .WithGroupName(nameof(ApiVersions.BaseServices))
               .WithOpenApi();

                app.MapPut("/PutUser", (HttpContext httpContext) =>
                {
                    return new
                    {
                        Id = $"{new Random().NextInt64(0, 999999):000000}",
                        Name = "Linyou",
                        Status = true,
                        Code = 200
                    };
                })
                 .WithName("PutUser")
                 .WithGroupName(nameof(ApiVersions.BaseServices))
                 .WithOpenApi();

                app.MapDelete("/DeleteUser", (HttpContext httpContext) =>
                {
                    return new
                    {
                        Id = $"{new Random().NextInt64(0, 999999):000000}",
                        Name = "Linyou",
                        Status = true,
                        Code = 200
                    };
                })
               .WithName("DeleteUser")
               .WithGroupName(nameof(ApiVersions.BaseServices))
               .WithOpenApi();
            }
        }
    }
}
